package com.android.xueshengzhouzhi;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;

import com.android.db.DBHelpter;
import com.android.model.Messageg;
import com.tencent.android.tpush.XGPushBaseReceiver;
import com.tencent.android.tpush.XGPushClickedResult;
import com.tencent.android.tpush.XGPushRegisterResult;
import com.tencent.android.tpush.XGPushShowedResult;
import com.tencent.android.tpush.XGPushTextMessage;

public class MsgReceiver extends XGPushBaseReceiver {
	Context context;
	Messageg messageg;

	@Override
	public void onDeleteTagResult(Context arg0, int arg1, String arg2) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onNotifactionClickedResult(Context arg0,
			XGPushClickedResult arg1) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onNotifactionShowedResult(Context arg0, XGPushShowedResult arg1) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onRegisterResult(Context arg0, int arg1,
			XGPushRegisterResult arg2) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onSetTagResult(Context arg0, int arg1, String arg2) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onTextMessage(Context arg0, XGPushTextMessage arg1) {
		// TODO Auto-generated method stub

		String mString = arg1.getContent();
		System.out.println("#### " + mString + " @@@");
		this.context = arg0;

		messageg = new Messageg(mString);
		DBHelpter dbHelpter = new DBHelpter(context);
		dbHelpter.Insert(messageg);

		showNotification();

	}

	@Override
	public void onUnregisterResult(Context arg0, int arg1) {
		// TODO Auto-generated method stub

	}

	@SuppressWarnings("deprecation")
	private void showNotification() {

		NotificationManager notificationManager = (NotificationManager) context
				.getSystemService(android.content.Context.NOTIFICATION_SERVICE);
		Notification notification = new Notification(R.drawable.icon, "学生周知",
				System.currentTimeMillis());
		notification.flags = Notification.FLAG_AUTO_CANCEL;
		notification.defaults |= Notification.DEFAULT_VIBRATE;
		notification.defaults |= Notification.DEFAULT_LIGHTS;
		long[] vibrate = { 0, 100, 200, 300 };
		notification.vibrate = vibrate;
		System.out.println(context.toString());
		PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
				new Intent(context, InfoListAct.class), 0);
		notification.setLatestEventInfo(context, "学生周知", messageg.GetTitle(),
				contentIntent);
		notificationManager.notify(112, notification);
	}
}
